package com.eduction.service.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eduction.bean.ExpertAndIdentity;
import com.eduction.common.dbutil.page.Page;
import com.eduction.modules.system.dao.SiteInspectionExpertGroupMapRspository;
import com.eduction.modules.system.dao.SiteInspectionGroupRepository;
import com.eduction.modules.system.dao.SiteInspectionSchoolGroupMapRepository;
import com.eduction.modules.system.entity.Expert;
import com.eduction.modules.system.entity.ExpertGroupMap;
import com.eduction.modules.system.entity.Group;
import com.eduction.modules.system.entity.School;
import com.eduction.modules.system.entity.SchoolGroupMap;
import com.eduction.modules.target.dao.SiteInspectionExpertRepository;
import com.eduction.service.SiteInspectionExpertService;

@Service
public class SiteInspectionExpertServiceIpl implements SiteInspectionExpertService {

	@Autowired
	private SiteInspectionExpertRepository siteInspectionExpertRepository;
	
	@Autowired
	private SiteInspectionSchoolGroupMapRepository  siteInspectionSchoolGroupMapRepository;
	
	@Autowired
	private SiteInspectionGroupRepository siteInspectionGroupRepository;
	
	@Autowired
	private SiteInspectionExpertGroupMapRspository siteInspectionExpertGroupMapRspository;
	
	@Override
	public Page<Expert> queryExpertByPage(Map<String, String> map) throws SQLException {
		
		return this.siteInspectionExpertRepository.selectExpertByPage(map);
	}

	@Override
	public void saveExpert(Expert e) {
		
		this.siteInspectionExpertRepository.insertExpert(e);
	}

	@Override
	public Expert queryExpertById(String id) throws SQLException {
	
		return this.siteInspectionExpertRepository.selectExpertById(id);
	}

	@Override
	public void removeExpert(String id) {
		
		this.siteInspectionExpertRepository.deleteExpert(id);
	}

	@Override
	public void saveGroup(Group group) {
		this.siteInspectionGroupRepository.saveGroup(group);
	}

	@Override
	public Page<Group> selectGroupByPage(Map<String, String> map,int type) throws SQLException {
		return this.siteInspectionGroupRepository.selectGroupByPage(map,type);
	}

	@Override
	public List<Expert> selectExpertIsNotGroup(String param) throws SQLException {
		return this.siteInspectionExpertRepository.selectExpertIsNotGroup(param);
	}

	

	@Override
	public void setGroupToSchool(SchoolGroupMap map) throws SQLException {
		this.siteInspectionSchoolGroupMapRepository.setGroupToSchool(map);
	}

	@Override
	public void setGroupToSchool2(SchoolGroupMap map) throws SQLException {
		this.siteInspectionSchoolGroupMapRepository.setGroupToSchool2(map);
	}

	@Override
	public Group selectGroupById(String id) throws SQLException {
		return this.siteInspectionGroupRepository.selectGroupById(id);
	}

	@Override
	public List<ExpertAndIdentity> selectExpertByGroup(String groupId) throws SQLException {
		
		return this.siteInspectionExpertRepository.selectExpertByGroup(groupId);
	}

	@Override
	public void updateIdentityInGroup(String groupId, String expertId, String identity) throws SQLException {
		this.siteInspectionExpertGroupMapRspository.updateIdentityInGroup(groupId, expertId, identity);
	}

	@Override
	public void saveExpertGroupMap(ExpertGroupMap e) throws SQLException {
		this.siteInspectionExpertGroupMapRspository.saveExpertGroupMap(e);
	}

	@Override
	public void saveExpertGroupMap2(ExpertGroupMap e) throws SQLException {
		this.siteInspectionExpertGroupMapRspository.saveExpertGroupMap2(e);
	}

	@Override
	public void deleteExpertInGroup(String expertId, String groupId) throws SQLException {
		this.siteInspectionExpertGroupMapRspository.deleteExpertInGroup(expertId, groupId);
		
	}

	@Override
	public void deleteExpertInGroup2(String expertId, String groupId) throws SQLException {
		this.siteInspectionExpertGroupMapRspository.deleteExpertInGroup2(expertId, groupId);

	}

	@Override
	public void deleteSchoolInGroup(String schoolId, String groupId) throws SQLException {
		// TODO Auto-generated method stub
		this.siteInspectionSchoolGroupMapRepository.deleteSchoolInGroup(schoolId, groupId);
	}

	@Override
	public void deleteSchoolInGroup2(String schoolId, String groupId) throws SQLException {
		// TODO Auto-generated method stub
		this.siteInspectionSchoolGroupMapRepository.deleteSchoolInGroup2(schoolId, groupId);
	}

	@Override
	public List<School> querySchoolByGroup(String groupId) throws SQLException {
		
		return this.siteInspectionSchoolGroupMapRepository.querySchoolByGroup(groupId);
	}

	@Override
	public void deleteGroup(String groupId,String groupType) throws SQLException{
		List<School> schoolList=querySchoolByGroup(groupId);
		List<ExpertAndIdentity> expertAndIdentityList=selectExpertByGroup(groupId);
		//删除专家与组关系
		//删除学校与组关系
		if("2".equals(groupType)){
			for(School s:schoolList){
				deleteSchoolInGroup(s.getP_id(),groupType);
			}
			for(ExpertAndIdentity e:expertAndIdentityList){
				deleteExpertInGroup(e.getPid(),groupType);
			}

		}else{
			for(School s:schoolList){
				deleteSchoolInGroup2(s.getP_id(),groupType);
			}
			for(ExpertAndIdentity e:expertAndIdentityList){
				deleteExpertInGroup2(e.getPid(),groupType);
			}
		}
		this.siteInspectionSchoolGroupMapRepository.deleteGroup(groupId);

	}

}
